(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.Messager 消息组件，弹出式消息提示
	 * @singleton
	 */
	HteOS.Messager = {
		tpl: HteOS.Template.compile('<div class="hte-messager-content">' + '<div class="hte-messager-title">' + '<h2><%=title%></h2>' + '</div>' + '<div class="hte-messager-body"></div>' + '<div class="hte-messager-buttons">' + '</div>' + '</div>')
	};

	/**
	 * 消息提示
	 *
	 * @param {String} title 标题
	 * @param {String} message 消息
	 * @param {Function} callback 回调函数
	 */
	HteOS.Messager.alert = function(title, message, callback) {
		var options = {};
		options.buttons = [{
			cls: 'btn-default',
			key: 'ok',
			text: '确定',
			autofocus: true
		}];
		options.title = title || '';
		options.message = message || '';
		options.callback = callback || null;
		HteOS.Messager.show(options);
	};

	/**
	 * 消息确认提示
	 *
	 * @param {String} title 标题
	 * @param {String} message 消息
	 * @param {Function} callback 回调函数
	 */
	HteOS.Messager.confirm = function(title, message, callback) {
		var options = {};
		options.buttons = [{
			cls: 'btn-default',
			key: 'yes',
			text: '确定'
		}, {
			cls: 'btn-default',
			key: 'no',
			text: '取消',
			autofocus: true
		}]
		options.title = title || '';
		options.message = message || '';
		options.callback = callback || null;
		HteOS.Messager.show(options);
	};
	
	/**
	 * 消息确认提示
	 *
	 * @param {String} title 标题
	 * @param {String} message 消息
	 * @param {Function} callback 回调函数
	 */
	HteOS.Messager.prompt = function(title, message, inputs,callback) {
		var options = {};
		options.buttons = [{
			cls: 'btn-default',
			key: 'yes',
			text: '确定'
		}, {
			cls: 'btn-default',
			key: 'no',
			text: '取消',
			autofocus: true
		}];
		html = "<div class=\"hte-messager-inputs\">";
		if(inputs != null){
			for(var i = 0;i <inputs.length;i++){
				var input = inputs[i];
				if(input.multiLine){
					html += "<textarea data-name=\""+input.name+"\" value=\""+(input.value|| "" )+"\"" + "rows=\"5\" style=\"width:100%;margin:5px 0px;\"" + 
							"class=\"hte-messager-input form-control \" placeholder=\""+(input.label || "")+"\"></textarea> ";
				}else{
					html += "<input data-name=\""+input.name+"\" value=\""+(input.value|| "" )+"\" style=\"width:100%;margin:5px 0px;\"" +
							"type=\""+(input.inputType || "text")+"\" class=\"hte-messager-input form-control \" placeholder=\""+(input.label || "")+"\"/> ";
				}
			}
		}
		html += "</div>"
		options.title = title || '';
		options.message = message + html;
		options.callback = callback || null;
		HteOS.Messager.show(options);
	};
	
	HteOS.Messager.showLoading = function(title,message){
		var options = {};
		options.title = title;
		var html = "<div class=\"hte-message-loading\">"
		+ "<div class=\"hte-message-progress-preload\"><img src="+HteOS.basePath+"images/preloader_48.gif /></div>"
		+ "<h3>"+message+"</h3>"
		"</div>";
		options.message = html;
		HteOS.Messager.show(options);
	}
	
	HteOS.Messager.progress = function(title,message,inline){
		var options = {},html;
		options.title = title;
		if(inline !== true){
		  html = "<div class=\"hte-message-progress\">"
			+ "<div class=\"hte-message-progress-preload\"><img src="+HteOS.basePath+"images/preloader_48.gif /></div>"
			+ "<h3 class=\"hte-message-progress-text\">"+message+"</h3>"
			"</div>";
		}else{
		 html = "<div class=\"hte-message-progress hte-message-progress-inline\">"
			+ "<div class=\"hte-message-progress-preload\"><img src="+HteOS.basePath+"images/preloader_32.gif /></div>"
			+ "<div class=\"hte-message-progress-text\">"+message+"</div>"
			"</div>";
		}
		options.message = html;
		HteOS.Messager.show(options);
	}
	
	HteOS.Messager.updateProgress = function(text){
		$(".hte-message-progress-text").text(text)
	}

	/**
	 * 显示消息提示
	 * @param {Object} options
	 */
	HteOS.Messager.show = function(options) {
		var el = $("<div class='hte-messager'>");
		el.html(this.tpl(options));
		el.find(".hte-messager-body").html(options.message);
		var buttons = options.buttons,
			content = el.find(".hte-messager-content");
		el.find(".hte-messager-buttons").append("<div class='hte-messager-tip' style='display: inline-block;'></div>")
		if (buttons && buttons.length > 0) {
			for (var i = 0; i < buttons.length; i++) {
				var b = buttons[i];
				var button = $("<button class='btn hte-messager-btn'>");
				button.addClass(b.cls);
				button.attr("data-key", b.key);
				button.attr("data-toggle", "hte-messager");
				if(b.icon){
					b.text = '<span class="'+b.icon+'"></span> ' + b.text;
				}
				button.html(b.text);
				if (b.autofocus) {
					button.attr("autofocus", "autofocus");
				}
				if (options.callback) {
					button.click(function(event) {
						var inputs = el.find(".hte-messager-input");
						var values = {};
						if(inputs.length > 0){
							inputs.each(function(){
								var input = $(this);
								values[input.data("name")] = input.val();
							});
						}
						var result = options.callback.call(this, $(this).data('key'),values);
						if(result !== false){
							el.fadeOut('fast',function(){
								el.remove();
							});
						}
						event.stopPropagation();
					});
				}else{
					button.click(function(event) {
						el.fadeOut('fast',function(){
							el.remove();
						});
						event.stopPropagation();
					});
				}
				el.find(".hte-messager-buttons").append(button);
			}
		}
		el.appendTo($(document.body));
		el.fadeIn();
		content.css("left", (el.width() - content.outerWidth()) / 2);
		content.css("top", (el.height() - content.outerHeight()) / 2);
	};
	
	HteOS.Messager.hide = function(){
		var el = $(".hte-messager");
		el.fadeOut('fast',function(){
			el.remove();
		});
	}
}));